import {
	USER_INFO,
	TOKEN,
	MATCHING_INFO
} from '@/config/cachekey';

const Cache = {
	
	keyPrev: 'store_',
	//设置缓存(expire为缓存时效)
	set(key, value, expire) {
		if (value === undefined || value === null) {
			console.error('Cache.set: value cannot be undefined or null');
			return false;
		}

		let data = {
			expire: expire ? (this.time() + expire) : "",
			value
		}
		
		if (typeof data === 'object')
			data = JSON.stringify(data);
		try {
			uni.setStorageSync(this.getKey(key), data)
			return true;
		} catch (e) {
			console.error('Cache.set: Failed to set storage', e);
			return false;
		}
	},
	get(key) {
		try {
			const data = uni.getStorageSync(this.getKey(key));
			if (!data) return false;

			const { value, expire } = JSON.parse(data);
			if (expire && expire < this.time()) {
				this.remove(key);
				return false;
			}
			return value;
		} catch (e) {
			console.error('Cache.get: Failed to get storage', e);
			return false;
		}
	},
	//获取当前时间
	time() {
		return Math.round(new Date() / 1000);
	},
	remove(key) {
		console.log('this.getKey(key)',this.getKey(key))
		if(key) uni.removeStorageSync(this.getKey(key))
	},
	getKey(key) {
		return this.keyPrev + key
	},
	// 批量设置缓存
	setMultiple(items) {
		if (!Array.isArray(items)) {
			console.error('Cache.setMultiple: items must be an array');
			return false;
		}

		try {
			items.forEach(({ key, value, expire }) => {
				this.set(key, value, expire);
			});
			return true;
		} catch (e) {
			console.error('Cache.setMultiple: Failed to set multiple storage', e);
			return false;
		}
	},
	// 批量获取缓存
	getMultiple(keys) {
		if (!Array.isArray(keys)) {
			console.error('Cache.getMultiple: keys must be an array');
			return false;
		}

		try {
			return keys.map(key => this.get(key));
		} catch (e) {
			console.error('Cache.getMultiple: Failed to get multiple storage', e);
			return false;
		}
	},
	clearExpired() {
		try {
			const keys = uni.getStorageInfoSync().keys;
			keys.forEach(key => {
				if (key.startsWith(this.keyPrev)) {
					this.get(key); // 调用 get 方法会自动清理过期缓存
				}
			});
			return true;
		} catch (e) {
			console.error('Cache.clearExpired: Failed to clear expired storage', e);
			return false;
		}
	},
	// 设置用户信息
	setUserInfo(userInfo) {
		return this.set(USER_INFO, userInfo);
	},

	// 获取用户信息
	getUserInfo() {
		return this.get(USER_INFO);
	},

	// 删除用户信息
	removeUserInfo() {
		return this.remove(USER_INFO);
	},

	// 设置 token
	setToken(token) {
		return this.set(TOKEN, token);
	},

	// 获取 token
	getToken() {
		return this.get(TOKEN);
	},

	// 删除 token
	removeToken() {
		return this.remove(TOKEN);
	},

	// 设置已邀请的陪伴师 ID
	setInvitedCoach(coachId) {
		const invitedCoaches = this.getInvitedCoaches();
		if (!invitedCoaches.includes(coachId)) {
			invitedCoaches.push(coachId);
			this.set('INVITED_COACHES', invitedCoaches);
		}
	},

	// 获取已邀请的陪伴师 ID 列表
	getInvitedCoaches() {
		return this.get('INVITED_COACHES') || [];
	},

	// 检查是否已邀请
	isCoachInvited(coachId) {
		const invitedCoaches = this.getInvitedCoaches();
		return invitedCoaches.includes(coachId);
	},

	// 设置匹配信息
	setMatchingInfo(matchingInfo) {
		return this.set(MATCHING_INFO, matchingInfo);
	},

	// 获取匹配信息
	getMatchingInfo() {
		return this.get(MATCHING_INFO);
	},

	// 删除匹配信息
	removeMatchingInfo() {
		return this.remove(MATCHING_INFO);
	},
}

export default Cache;
